package ymbky.firstcodesunnyweather.planbook

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {
    private var DailyplanList= ArrayList<Record>()
//    private var mDailyplanList= ArrayList<Record>()
    private var RdDbopenHelper: RecordDbOpenHelper? = null
//    private var mDailyplanAdapter:DailyplanAdapter?=null
    lateinit var viewModel: MainViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel=ViewModelProvider(this).get(MainViewModel::class.java)
        setSupportActionBar(toolbar)
        onClick.setOnClickListener {
            val intent= Intent(this,AddActivity::class.java)
            startActivity(intent)
        }
        initData()
        initEvent()
        refreshDataFromDb()

//        initDailyplanList()
//        val layoutInflater= LinearLayoutManager(this)
//        rVDailyPlan.layoutManager=layoutInflater
//        val adapter=DailyPlanAdapter(DailyplanList)
//        rVDailyPlan.adapter=adapter

    }
//   重点在这里 这个refresh 我改了好几次了都不行 现在就是我添加的数据 在数据库也添加成功了但是
//   不能立即刷新出来 需要退出重新打开才能刷新出来  我把刷新函数写在了onResume里 但是还没用  我改了三个小时了各种方法
//   都试了 但是就是不行

    private fun refreshDataFromDb() {
        RdDbopenHelper =RecordDbOpenHelper(this)
       DailyplanList= dataFromDB as ArrayList<Record>
        viewModel.getRecord(DailyplanList)
        viewModel.record.observe(this, androidx.lifecycle.Observer{ count->count })

    }
//  以上这几行代码是关键  其他基本运行正常但是就是刷新不了

    private fun initEvent() {

        val adapter=DailyplanAdapter(this,DailyplanList)
        rVDailyPlan.adapter=adapter
        val layoutManager=LinearLayoutManager(this)
        rVDailyPlan.layoutManager=layoutManager


    }

    private fun initData() {

       RdDbopenHelper =RecordDbOpenHelper(this)
        DailyplanList = dataFromDB as ArrayList<Record>



    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when(item.itemId) {
            R.id.dailyPlan -> {
                val intent = Intent(this, DailyplanActivity::class.java)
                startActivity(intent)
            }
            R.id.longPlan -> {
                val intent = Intent(this, LongplanActivity::class.java)
                startActivity(intent)
            }
            R.id.myIdeal -> {
                val intent = Intent(this, MyidealActivity::class.java)
                startActivity(intent)
            }
        }
        return true
    }

    private val dataFromDB: List<Record>
         get() = RdDbopenHelper!!.queryAllFromDb()


//    private val getCurrentTimeFormat: String
//        @SuppressLint("SimpleDateFormat")
//        get(){
//            val simpleDataFormat=SimpleDateFormat("yyyy年MM月dd日 HH：mm：ss")
//            val date= Date()
//            return simpleDataFormat.format(date)
//        }

}